package com.yt.setclaim.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yt.setclaim.models.entity.MedicalReview;
import com.yt.setclaim.models.entity.ClaimInfo;
import com.yt.setclaim.models.service.MedicalReviewService;
import com.yt.setclaim.models.vo.ClaimInfoVO;
import com.yt.setclaim.server.mapper.MedicalReviewMapper;
import com.yt.setclaim.server.mapper.ClaimInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

// 添加Dubbo服务引用相关导入
import org.apache.dubbo.config.annotation.DubboReference;
import com.yt.sys.models.service.ISysAdminService;
import com.yt.sys.models.entity.SysAdmin;

@Service
public class MedicalReviewServiceImpl extends ServiceImpl<MedicalReviewMapper, MedicalReview> implements MedicalReviewService {
    
    private final ClaimInfoMapper claimInfoMapper;
    
    // 添加对系统管理员服务的远程引用
    @DubboReference(check = false)
    private ISysAdminService sysAdminService;
    
    public MedicalReviewServiceImpl(ClaimInfoMapper claimInfoMapper) {
        this.claimInfoMapper = claimInfoMapper;
    }
    
    @Override
    public MedicalReview getByClaimId(Integer claimId) {
        return this.getOne(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<MedicalReview>()
                .eq("claim_id", claimId)
                .eq("is_delete", 0));
    }

    @Override
    public List<ClaimInfoVO> getPendingClaims() {
        // 获取状态为"待审核"(status_attr_id=21)且尚未进行医疗审核的索赔记录
        QueryWrapper<ClaimInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status_attr_id", 21); // 待审核状态
        queryWrapper.eq("is_delete", 0); // 未删除
        
        // 获取所有待审核的索赔记录
        List<ClaimInfo> claimInfoList = claimInfoMapper.selectList(queryWrapper);
        
        // 转换为ClaimInfoVO
        List<ClaimInfoVO> claimInfoVOList = new ArrayList<>();
        for (ClaimInfo claimInfo : claimInfoList) {
            // 检查是否已经进行了医疗审核
            MedicalReview existingReview = this.getOne(new QueryWrapper<MedicalReview>()
                    .eq("claim_id", claimInfo.getClaimId())
                    .eq("is_delete", 0));
            
            // 如果尚未进行医疗审核，则添加到待审核列表中
            if (existingReview == null) {
                ClaimInfoVO vo = new ClaimInfoVO();
                BeanUtils.copyProperties(claimInfo, vo);
                // ID转换为字符串类型
                vo.setClaimId(String.valueOf(claimInfo.getClaimId()));
                vo.setPolicyNo(String.valueOf(claimInfo.getPolicyId())); // 简化处理，实际应关联保单表获取保单号
                vo.setUserName("用户" + claimInfo.getUserId()); // 简化处理，实际应关联用户表获取用户名
                vo.setTotalAmount(claimInfo.getTreatmentAmount());
                vo.setClaimStatus("待审核"); // 简化处理，实际应关联属性表获取状态名称
                claimInfoVOList.add(vo);
            }
        }
        
        return claimInfoVOList;
    }
    
    /**
     * 校验管理员ID是否存在
     * @param adminId 管理员ID
     * @return 管理员是否存在
     */
    public boolean isValidAdminId(Integer adminId) {
        if (adminId == null) {
            return false;
        }
        
        try {
            // 尝试通过远程服务获取管理员信息
            SysAdmin admin = sysAdminService.getById(adminId);
            return admin != null;
        } catch (Exception e) {
            // 如果远程服务调用失败，返回false
            return false;
        }
    }
}